package com.zzxz.practicaltrainingsys.mapper;

import com.zzxz.practicaltrainingsys.entity.ChatRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ChatManageMapper {

    /**
     * 获取所有聊天记录
     */
    @Select("SELECT * FROM chat_record ORDER BY create_time DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    List<ChatRecord> selectAllChatRecords();

    /**
     * 根据邀请码获取群聊消息
     */
    @Select("SELECT * FROM chat_record WHERE invite_code = #{inviteCode} ORDER BY create_time ASC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    List<ChatRecord> selectGroupMessages(@Param("inviteCode") String inviteCode);

    /**
     * 根据ID删除聊天记录
     */
    @Delete("DELETE FROM chat_record WHERE id = #{id}")
    int deleteChatRecordById(@Param("id") Long id);

    /**
     * 批量删除聊天记录
     */
    @Delete("<script>" +
            "DELETE FROM chat_record WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchDeleteChatRecords(@Param("ids") List<Long> ids);

    /**
     * 根据邀请码删除群聊
     */
    @Delete("DELETE FROM chat_record WHERE invite_code = #{inviteCode}")
    int deleteGroupChatByInviteCode(@Param("inviteCode") String inviteCode);

    /**
     * 清理指定天数前的历史消息
     */
    @Delete("DELETE FROM chat_record WHERE create_time < #{cutoffTime}")
    int clearOldMessages(@Param("cutoffTime") LocalDateTime cutoffTime);

    /**
     * 保存聊天记录
     */
    @Insert("INSERT INTO chat_record (username, real_name, user_type, content, chat_type, invite_code, create_time) " +
            "VALUES (#{username}, #{realName}, #{userType}, #{content}, #{chatType}, #{inviteCode}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertChatRecord(ChatRecord chatRecord);

    /**
     * 根据条件查询聊天记录
     */
    @Select("<script>" +
            "SELECT * FROM chat_record WHERE 1=1 " +
            "<if test='userType != null and userType != \"\"'>" +
            "AND user_type = #{userType} " +
            "</if>" +
            "<if test='chatType != null and chatType != \"\"'>" +
            "AND chat_type = #{chatType} " +
            "</if>" +
            "<if test='username != null and username != \"\"'>" +
            "AND (username LIKE CONCAT('%', #{username}, '%') OR real_name LIKE CONCAT('%', #{username}, '%')) " +
            "</if>" +
            "<if test='inviteCode != null and inviteCode != \"\"'>" +
            "AND invite_code LIKE CONCAT('%', #{inviteCode}, '%') " +
            "</if>" +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND (content LIKE CONCAT('%', #{keyword}, '%') " +
            "OR username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR real_name LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "ORDER BY create_time DESC" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    List<ChatRecord> searchChatRecords(
            @Param("userType") String userType,
            @Param("chatType") String chatType,
            @Param("username") String username,
            @Param("inviteCode") String inviteCode,
            @Param("keyword") String keyword
    );

    /**
     * 根据ID查询单条聊天记录
     */
    @Select("SELECT * FROM chat_record WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    ChatRecord selectChatRecordById(@Param("id") Long id);
}